local mou__luanji = fk.CreateSkill {

  name = "joy_mou__luanji",

  tags = {  },

}



mou__luanji:addEffect("viewas", {
  name = "joy_mou__luanji",
  anim_type = "offensive",
  pattern = "archery_attack",
  prompt = "#mou__luanji-viewas",
  enabled_at_play = function(self, player)
    return true
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected == 2 or Fk:currentRoom():getCardArea(to_select) ~= Player.Hand then return false end
    local record = type(player:getMark("@joymou__luanji-turn")) == "table" and player:getMark("@joymou__luanji-turn") or {}
    return not table.contains(record, Fk:getCardById(to_select):getColorString())
  end,
  view_as = function(self, player,cards)
    if #cards == 2 then
      local archery_attack = Fk:cloneCard("archery_attack")
      archery_attack:addSubcards(cards)
      return archery_attack
    end
  end,
  before_use = function(self, player, use)
    local room = player.room
    local record = type(player:getMark("@joymou__luanji-turn")) == "table" and player:getMark("@joymou__luanji-turn") or {}
    local cards = use.card.subcards
    for _, cid in ipairs(cards) do
      local color = Fk:getCardById(cid):getColorString()
      if color ~= "nocolor" then table.insertIfNeed(record, color) end
    end
    room:setPlayerMark(player, "@joymou__luanji-turn", record)
  end
})

mou__luanji:addEffect(fk.CardResponding, {
  name = "#joy_mou__luanji_trigger",
  anim_type = "drawcard",
  events = {fk.CardResponding,fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardResponding and player:hasSkill(mou__luanji.name) and player:getMark("joy_mou__luanjidraw-turn") < 3 and data.card.name == "jink" then
      return data.responseToEvent and
      data.responseToEvent.card.name =="archery_attack"
    elseif player:hasSkill(mou__luanji.name) and player:getMark("joy_mou__luanjidiscard-turn") < 3 and data.card then
      return target == player and
      data.card.name =="archery_attack" and not data.to.dead  and not data.to:isNude()
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.CardResponding then return true end
    local room = player.room
    return room:askForSkillInvoke(player,mou__luanji.name,".","#joy_mou__luanjidiscard::"..data.to.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardResponding and not player.dead then
      player:broadcastSkillInvoke(mou__luanji.name)
      player:drawCards(1, mou__luanji.name)
      room:addPlayerMark(player,"joy_mou__luanjidraw-turn",1)
    else
      local card = room:askForCardChosen(player,data.to,"he",mou__luanji.name,"#joy_mou__luanjidiscardchosen")
      if card > 0 then
        room:throwCard(card,mou__luanji.name,data.to,player)
        room:addPlayerMark(player,"joy_mou__luanjidiscard-turn",1)
      end
    end
  end,
})
mou__luanji:addEffect(fk.Damage, {
  name = "#joy_mou__luanji_trigger",
  anim_type = "drawcard",
  events = {fk.CardResponding,fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardResponding and player:hasSkill(mou__luanji.name) and player:getMark("joy_mou__luanjidraw-turn") < 3 and data.card.name == "jink" then
      return data.responseToEvent and
      data.responseToEvent.card.name =="archery_attack"
    elseif player:hasSkill(mou__luanji.name) and player:getMark("joy_mou__luanjidiscard-turn") < 3 and data.card then
      return target == player and
      data.card.name =="archery_attack" and not data.to.dead  and not data.to:isNude()
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.CardResponding then return true end
    local room = player.room
    return room:askForSkillInvoke(player,mou__luanji.name,".","#joy_mou__luanjidiscard::"..data.to.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardResponding and not player.dead then
      player:broadcastSkillInvoke(mou__luanji.name)
      player:drawCards(1, mou__luanji.name)
      room:addPlayerMark(player,"joy_mou__luanjidraw-turn",1)
    else
      local card = room:askForCardChosen(player,data.to,"he",mou__luanji.name,"#joy_mou__luanjidiscardchosen")
      if card > 0 then
        room:throwCard(card,mou__luanji.name,data.to,player)
        room:addPlayerMark(player,"joy_mou__luanjidiscard-turn",1)
      end
    end
  end,
})

return mou__luanji